import configdata from './config'
import api from './httpLbApi.js'
var version = '1.0'
module.exports = {
    // post请求
    post(url, data) {
        url = configdata.APIHOST + url;//请求地址+接口
        return new Promise((resolve, reject) => {
            uni.showLoading({
                title: '加载中',
                icon: 'none'
            })
            uni.request({
                url: url + '?token=' + uni.getStorageSync('USERINFO'),
                data: data,
                method: 'POST',
                header: {
                    "Authorization": 'Bearer ' + uni.getStorageSync('USERINFO'),
                },
                success: (res) => {
                    uni.hideLoading()
                    if (res.data.code == 200) {
                        resolve(res.data);
                    } else if (res.data.code == 400) {
                        resolve(res.data);
                        uni.showToast({
                            title: res.data.msg,
                            icon: 'none'
                        });
                    } else if (res.data.code == -1) {
                        resolve(res.data);
                        uni.setStorageSync("USERINFO", '')
                        uni.showToast({
                            title: res.data.message,
                            icon: 'none'
                        });

                        setTimeout(() => {
                            uni.redirectTo({
                                url: '/userPage/login/login'
                            })
                        }, 400)
                    }
                    else {
                        uni.showToast({
                            title: res.data.msg,
                            icon: 'none'
                        });
                        resolve(res.data);
                    }
                },
                fail: (err) => {
                    reject(err);
                }
            })
        })
    },
    get(url, data) {
        url = configdata.APIHOST + url;
        return new Promise((resolve, reject) => {
            uni.request({
                url: url + '?token=' + uni.getStorageSync('USERINFO'),
                data: data,
                method: 'GET',
                header: {
                    "Authorization": 'Bearer ' + uni.getStorageSync('USERINFO'),
                },

                success: (res) => {
                    if (res.data.code == 200) {
                        resolve(res.data);
                    } else if (res.data.code == 400) {
                        resolve(res.data);
                        uni.showToast({
                            title: res.data.msg,
                            icon: 'none'
                        });
                    } else if (res.data.code == -1) {
                        resolve(res.data);
                        uni.setStorageSync("USERINFO", '')
                        uni.showToast({
                            title: res.data.message,
                            icon: 'none'
                        });

                        setTimeout(() => {
                            uni.redirectTo({
                                url: '/userPage/login/login'
                            })
                        }, 400)
                    }
                    else {
                        uni.showToast({
                            title: res.data.msg,
                            icon: 'none'
                        });
                        resolve(res.data);
                    }
                },
                fail: (err) => {
                    reject(err);
                }
            })
        })
    },



    /*
	 *   上传图片
	 * 		successFn  //回调
	 * 		num		  	//选择图片的数量
	 * 		data		//上传的参数
	 * 		progressFn	//进度的回调
	 */
    uploadImg(successFn, num = 1, data = {}, progressFn) { //上传图片
        let url = configdata.APIHOST;
        uni.chooseImage({
            count: num, //默认1
            sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], //从相册选择
            // header: {
            //     "Authorization": 'Bearer ' + uni.getStorageSync('USERINFO'),
            //     'timestamp': new Date().getTime(),
            //     // "sign":signature
            // },
            success: function (res) { //选择后
                ////console.log(res, '选择的图片');
                /*uni.showLoading({
                    title:'上传中...'
                })*/
                const tempFilePaths = res.tempFilePaths;
                let fileList = {}; //储存上传实例；
                for (var i in tempFilePaths) {
                    fileList['uploadTask' + i] = uni.uploadFile({ //上传后台
                        url: url +  uni.getStorageSync('USERINFO'),
                        filePath: tempFilePaths[i],
                        name: 'file[]',
                        success: function (upRes) { //上传成功
                            if (upRes.statusCode == 200) {
                                let data = JSON.parse(upRes.data);
                                if (typeof (successFn) == 'function') {
                                    successFn(data)
                                }
                            } else {
                                uni.showToast({
                                    title: upRes.errMsg,
                                    icon: 'none'
                                })
                            }

                        },
                        fail: function (err) {
                            ////console.log('上传失败',err);
                            uni.showToast({
                                title: '图片上传失败，请稍后再试.' + err.errMsg,
                                icon: 'none'
                            })
                        }
                    })
                    //监听进度
                    fileList['uploadTask' + i].onProgressUpdate(function (res) {
                        if (typeof (progressFn) == 'function') {
                            progressFn(res)
                        }
                        if (res.progress < 99.99) {
                            uni.showLoading({
                                title: res.progress + '%'
                            });
                        } else {
                            uni.hideLoading()
                        }
                        ////console.log('上传进度' + res.progress);
                        ////console.log('已经上传的数据长度' + res.totalBytesSent);
                        ////console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
                    });
                };
            } //选择成功——end
        });
    }
}